package com.madding.shared.test.httpclient;

import org.apache.commons.httpclient.DefaultMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;

public class MyHttpClientTest3_1 {

    private final static MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
    private final static HttpClient httpclient = new HttpClient(manager);

    public static void main(String[] args) {
        System.setProperty("javax.net.debug", "ssl,handshake");
        System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol");

        httpclient.getParams().setParameter("http.socket.timeout", 1000);
        httpclient.getParams().setParameter("http.connection.timeout", 1000);
        httpclient.getParams().setParameter("http.connection-manager.timeout", 60*60L);
//        httpclient.getParams().setParameter("http.protocol.head-body-timeout", 1000);
//        httpclient.setConnectionTimeout(1000);
//        httpclient.setTimeout(1000);
//        httpclient.setHttpConnectionFactoryTimeout(1000);

        for (int i = 0; true; i++) {
            new Thread(new MyRunnable(httpclient), "trhead-" + i).start();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
        }
    }

}

class MyRunnable implements Runnable {

    HttpClient httpclient = null;

    MyRunnable(HttpClient client){
        httpclient = client;
    }

    public void run() {
        GetMethod getMethod = new GetMethod("http://www.apache.org/");
        // initQueryString(getMethod, currentLoginId, memberId, offerId);

        try {
            DefaultMethodRetryHandler retryHandler = new DefaultMethodRetryHandler();
            retryHandler.setRetryCount(0);
            getMethod.setMethodRetryHandler(retryHandler);
            
            
            int statusCode = httpclient.executeMethod(getMethod);
            if (statusCode == HttpStatus.SC_OK) {
                getMethod.getResponseBodyAsString();
            }
        } catch (Exception e) {
            System.err.println(e);
        } finally {
            getMethod.releaseConnection();
        }
    }
}
